import matplotlib.pyplot as plt
import Image
import cv2
import sys

from skimage import exposure
import skimage.io
import skimage.util
from skimage.io.collection import ImageCollection
import numpy as np
import json
#from Data_utils.Metrics import PSNR
import keras.backend as K
import sys
""""
def check_adjust_img():
    img_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/decsai/GM512/src/1.pgm'
    img = skimage.io.imread(img_path)
    print img.shape
    skimage.io.imshow(img)
    skimage.io.show()
    img_ = skimage.exposure.adjust_gamma(img,3)
    skimage.io.imshow(img_)
    skimage.io.show()
    img_ = skimage.util.random_noise(image=img,var=((25/255.)**2)*np.random.uniform(0,1))
    skimage.io.imshow(img_)
    skimage.io.show()

def test_json():
    f = file('Conv/metadata/darken_training.json','rb')
    print f.readlines()
    training = json.dumps(f,default=lambda o: o.__dict__, indent=4)
    print training

    meta_json = json.dumps(training, default=lambda o: o.__dict__, indent=4)
    f.write(meta_json)
    f.close()


def test_PSNR():
    a = np.random.randint(0,255,(512,512))
    a = np.float32(a)
    #b = np.random.randint(0,255,(512,512))
    b = skimage.exposure.adjust_gamma(a,3)
    print PSNR(a,b)

def test_flat():
    a = np.random.randint(0,255,(3,3,3))
    b = np.reshape(a,(a.shape[0],9))
    print b.shape
    print a
    print b

def test_sum_aixs():
    a = np.random.randint(0,255,(3,4))
    b = np.sum(a,axis=0)
    c = np.sum(a,axis=1)
    d = np.sum(a,axis=-1)
    e = np.mean(a,axis=1)
    print b
    print c
    print d
    print e

def get_tensor_shape():
    a = np.random.randint(0,255,(3,4))
    b = K.variable(a)
    print K.eval(b.shape[0])

def test_var():
    a = ((25/255.)**2)*np.random.uniform(0,1)
    print a

def test_repeat():
    a = np.random.randint(0,10,(4,4))
    b = np.repeat(a,2,axis=1)
    c = np.repeat(b,2,axis=0)
    img_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/decsai/GM512/inf/Girl.pgm'
    img_arr = skimage.io.imread(img_path)
    img_arr_repeat = np.repeat(img_arr,2,axis=0)
    img_arr_repeat = np.repeat(img_arr_repeat,2,axis=1)
    #plt.subplot(2,1,1)
    skimage.io.imshow(img_arr)
    skimage.io.show()
    plt.hold()
    #plt.subplot(2,1,2)
    skimage.io.imshow(img_arr_repeat)
    skimage.io.show()

def test_log():
    a = np.random.randint(0,10000)
    b = np.log10(a)
    c = np.log(a)/np.log(10)
    print b,c

def test_func():
    a = np.random.randint(-255,255,(1000,))
    b = (1/1.01)**a
    plt.plot(b)
    plt.show()

def test_sys():
    print sys.path
"""
def test_img_as_ubyte():
    img_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/decsai/GM512/patches18_val/2_sar-large.pgm'
    img_arr = skimage.io.imread(img_path)
    print img_arr
    img_arr_noised = skimage.util.random_noise(img_arr)
    print img_arr_noised
    img_arr_noised_u = skimage.util.img_as_ubyte(np.asarray(img_arr_noised * 255,dtype=np.uint8))
    print img_arr_noised_u

if __name__ == '__main__':
    test_img_as_ubyte()
